package com.obas.plugin {
   import com.ease.core.managers.PerspectiveManager;
   import com.ease.core.managers.ViewManager;
   import com.ease.core.plugin.PluginInfo;
   import com.obas.constants.AppConstants;
   import com.ease.plugin.PluginFactory;
   import com.ease.plugin.URLResource;
   import com.ease.plugin.events.FactoryEvent;

   import flash.events.Event;
   import flash.events.EventDispatcher;

   public class PluginManager extends EventDispatcher{

      private static var pluginCount:int = 0;

      public function loadPlugin(urls:Array):void{
         pluginCount = urls.length;
         var tempCount:int = 0;
         for each(var url:String in urls){
            var factory:PluginFactory = new PluginFactory();
            factory.addEventListener(FactoryEvent.PLUGIN_LOAD,onPluginLoad);

            var myPluginURL:URLResource = new URLResource(url);
            factory.loadPlugin(myPluginURL);
         }

         function onPluginLoad(event:FactoryEvent):void{
            var pluginInfo:PluginInfo = event.resource.pluginInfo;
            if (pluginInfo) {
               pluginInfo.start();
            }

            tempCount++;
            if(tempCount == pluginCount){
               dispatchEvent(new Event(Event.COMPLETE, false, false));
            }
         }
      }

      private static var _instance:PluginManager = null;
      public static function getInstance():PluginManager {
         if (!_instance) {
            _instance = new PluginManager();
         }
         return _instance;
      }

      public function PluginManager() {
         if (_instance) {
            throw new Error("PluginManager is singleton");
         }
      }
   }
}